package com.example.redisrabbitesintegeation.web.comtroller;

import com.example.redisrabbitesintegeation.common.config.ResultVO;
import com.example.redisrabbitesintegeation.model.entity.News;
import com.example.redisrabbitesintegeation.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName NewsController
 * @Description TODO
 * @Author Kx
 * @Date 2025/10/21 20:01
 * @Version 1.0
 */
@RestController
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private NewsService newsService;

    /**
     * @author: kx
     * @date: 2025/10/21 20:02
     * @param news : 新闻对象
     * @return com.example.redisrabbitesintegeation.common.config.ResultVO<java.lang.String>
     * @description: 发布新闻
    */
    @PostMapping("/publish")
    public ResultVO<String> publishNews(@RequestBody News news) {
        newsService.publishNews(news);
        return ResultVO.success("添加成功");
    }

    /**
     * @author: kx
     * @date: 2025/10/21 20:07
     * @return com.example.redisrabbitesintegeation.common.config.ResultVO<java.util.List<com.example.redisrabbitesintegeation.model.entity.News>>
     * @description: 查询所有新闻信息
    */
    @GetMapping("/news")
    public ResultVO<List<com.example.redisrabbitesintegeation.model.document.News>> getNewsList() {
        List<com.example.redisrabbitesintegeation.model.document.News> newsList = newsService.getNewsList();
        return ResultVO.success(newsList);
    }

    /**
     * @author: kx
     * @date: 2025/10/21 20:14
     * @param keyword : 关键字
     * @return com.example.redisrabbitesintegeation.common.config.ResultVO<java.util.List<com.example.redisrabbitesintegeation.model.entity.News>>
     * @description: 搜索新闻信息并高亮显示
    */
    @GetMapping("/search/{keyword}")
    public ResultVO<List<com.example.redisrabbitesintegeation.model.document.News>> searchNewsWithHighlight(@PathVariable("keyword") String keyword) {
        List<com.example.redisrabbitesintegeation.model.document.News> newsList = newsService.searchNewsWithHighlight(keyword);
        return ResultVO.success(newsList);
    }

    /**
     * @author: kx
     * @date: 2025/10/21 20:36
     * @param newsId : 新闻id
     * @return com.example.redisrabbitesintegeation.model.entity.News
     * @description: 根据新闻id查询新闻信息
    */
    @GetMapping("/news/{newsId}")
    public ResultVO<News> getNewsById(@PathVariable("newsId") Integer newsId) {
        return ResultVO.success(newsService.getNewsById(newsId));
    }

    /**
     * @author: kx
     * @date: 2025/10/22 19:05
     * @param newsId : 新闻id
     * @return com.example.redisrabbitesintegeation.common.config.ResultVO<java.lang.String>
     * @description: 删除新闻
    */
    @DeleteMapping("/news/{newsId}")
    public ResultVO<String> deleteNews(@PathVariable("newsId") Integer newsId) {
        newsService.deleteNewsById(newsId);
        return ResultVO.success("删除成功");
    }
}
